package org.eclipse.swt.snippets;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageFilter implements Filter {

    private String  regex;
    private Pattern pattern;

    @Override
    public boolean filter(Message msg) {
        if (regex == null) {
            return true;
        }
        Matcher matcher = pattern.matcher(msg.getMessage());
        return matcher.find();
    }

    @Override
    public String toString() {
        return regex;
    }

    /**
     * @return the pattern
     */
    public String getPattern() {
        return regex;
    }

    /**
     * @param pattern
     *            the pattern to set
     */
    public void setPattern(String regex) {
        this.regex = regex;
        pattern = Pattern.compile(regex);
    }

    @Override
    public int hashCode() {
        int hashCode = MessageFilter.class.hashCode();
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof MessageFilter) {
            MessageFilter f = (MessageFilter) obj;
            return hashCode() == f.hashCode();
        }
        return false;
    }
}
